package com.doublefish.doublefishrobot.utils;

/**
 * 作者:dugaofeng on 2024/10/17 11:02
 */
public class VersionCompareUtils {

    // 99.无法比较  0.相等  -1.递减  1.递增
    public static int compare(String ver0, String ver1) {
        if (ver0 == null || ver1 == null || ver0.length() == 0 || ver1.length() == 0) {
            return 99; //无法比较
        }
        ver0 = ver0.trim();
        ver1 = ver1.trim();

        if (ver0.equals(ver1)) {
            return 0;   // 相等
        }

        String[] arr0 = ver0.split("\\.");
        String[] arr1 = ver1.split("\\.");
        int minLength = Math.min(arr0.length, arr1.length);

        for (int i = 0; i < minLength; i++) {
            int n0 = Integer.parseInt(arr0[i]);
            int n1 = Integer.parseInt(arr1[i]);
            if (n0 > n1) {
                return -1;
            } else if (n0 < n1) {
                return 1;
            }
        }

        if (arr0.length == arr1.length) {
            return 0;
        }
        if (arr0.length > arr1.length) {
            return -1;
        }
        return 1;
    }
}
